package com.bbm.bbmanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbm.bbmanage.entity.HouseImg;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.service.HouseImgService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (HouseImg)表控制层
 *
 * @author lionkliu
 * @since 2023-12-11 14:04:23
 */
@RestController
@RequestMapping("houseImg")
public class HouseImgController {

    @Resource
    private HouseImgService houseImgService;

    @GetMapping("/{id}")
    public ResponseResult getImagesByHouse(@PathVariable Integer id) {
        LambdaQueryWrapper<HouseImg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseImg::getHouseId, id);
        List<HouseImg> images = houseImgService.list(queryWrapper);
        List<String> list = images.stream().map(HouseImg::getImg).collect(Collectors.toList());
        return ResponseResult.okResult(list);
    }

    @GetMapping("/house/{id}")
    public ResponseResult getImagesVoByHouseId(@PathVariable Long id) {
        return houseImgService.getImagesVoByHouseId(id);
    }


    @GetMapping("/page")
    public ResponseResult selectPage(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize) {
        return houseImgService.selectPage(pageNum, pageSize);
    }

    @PostMapping
    public ResponseResult insert(@RequestBody HouseImg houseImg) {
        return houseImgService.insertOne(houseImg);
    }

    @PutMapping
    public ResponseResult update(@RequestBody HouseImg houseImg) {
        return houseImgService.updateOne(houseImg);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        return houseImgService.deleteById(id);
    }

}
